#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int n = nums.size(), ret = 0;
        if (n < 3)
            return 0;
        vector<vector<int>> dp(n, vector<int>(n, 0));
        unordered_map<long long, vector<int>> hash;
        hash[nums[0]].push_back(0);
        for (int i = 1; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                long long tmp = (long long)nums[i] * 2 - nums[j];
                if (hash.count(tmp)) {
                    for (auto& e : hash[tmp]) {
                        dp[i][j] += dp[e][i] + 1;
                    }
                }
                ret += dp[i][j];
            }
            hash[nums[i]].push_back(i);
        }
        return ret;
    }
};